/**
 * BillManager.java
 * com.jamin.aabill.manager
 *
 * Function： TODO 
 *
 *   ver     date      		author
 * ──────────────────────────────────
 *   		 2012-9-4 		wangjieming
 *
 * Copyright (c) 2012, TNT All Rights Reserved.
 */

package com.jamin.aabill.manager;

import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;

import com.jamin.aabill.dao.BillAndDetailDao;
import com.jamin.aabill.data.Bill;
import com.jamin.aabill.data.BillDetail;
import com.jamin.aabill.data.BillViewDetail;
import com.jamin.aabill.db.BillDetailTableMetaData;
import com.jamin.aabill.db.BillTableMetaData;
import com.jamin.aabill.db.BillViewMetaData;
import com.jamin.aabill.utils.Utils;

/**
 * ClassName:BillManager
 *
 * @author   wangjieming
 * @version  1.0
 * @since    v1.0
 * @Date	 2012-9-4 上午11:36:28 
 */
public class BillManager {

	private static final String TAG = "BillManager=======wangjieming";

	public BillManager(){

	}




	/**
	 * 创建等额账单<br>
	 * @method saveEqualBillList
	 * @param project
	 * @param circleId
	 * @param type
	 * @param money
	 * @param payList
	 * @param profitList
	 * @throws 
	 * @since v1.0
	 */
	public void saveEqualBillList(long createtime , String project , long circleId , int type , double money , 
			ArrayList<String> payList , ArrayList<String> profitList) {
		List<BillDetail> billDetailList = new ArrayList<BillDetail>();
		Bill mBill = new Bill(circleId, project , type, "", System.currentTimeMillis() , createtime);
		double payMoney = money / payList.size();
		for(int i = 0 ; i < payList.size(); i++){
			billDetailList.add(new BillDetail(Long.parseLong(payList.get(i)) ,  payMoney));
		}
		double profitMoney = - money / profitList.size();
		for(int i = 0 ; i < profitList.size(); i++){
			billDetailList.add(new BillDetail(Long.parseLong(profitList.get(i)) ,  profitMoney));
		}
		BillAndDetailDao mBillDao = new BillAndDetailDao();
		mBillDao.insert(mBill, billDetailList);
	}




	/**
	 * 得到账单列表<br>
	 * @method getBillList
	 * @return
	 * @throws 
	 * @since v1.0
	 */
	public List<Bill> getBillList(long circleId){
		List<Bill> billList = new ArrayList<Bill>();
		BillAndDetailDao mDao = new BillAndDetailDao();
		Cursor cur = mDao.getBillList(circleId);
		for(cur.moveToFirst() ; !cur.isAfterLast(); cur.moveToNext()){
			billList.add(new Bill(cur));
		}
		cur.close();
		return billList;
	}

	/**
	 * 查询Circle的总金额<br>
	 * @method getCircleTotalCost
	 * @param Circle_Id
	 * @return
	 * @throws 
	 * @since v1.0
	 */
	public double getCircleTotalCost(long Circle_Id){
		String where = BillViewMetaData.CIRCLE_ID + "=" + Circle_Id + " and " + BillViewMetaData.MONEY + ">" + 0;
		return new BillAndDetailDao().getTotalCost(where);
	}

	/**
	 * 查询单个Bill的总金额<br>
	 * @method getBillTotalCost
	 * @param BillId
	 * @return
	 * @throws 
	 * @since v1.0
	 */
	public double getBillTotalCost(long BillId){
		String where = BillViewMetaData.BILL_ID + "=" + BillId + " and " + BillViewMetaData.MONEY + ">" + 0;
		return new BillAndDetailDao().getTotalCost(where);
	}


	/**
	 * 得到每个人的金额<br>
	 * @param BillId
	 * @return
	 */
	public double getMemberTotalCost(long circleMemberId){
		String where = BillViewMetaData.CIRCLE_MEMBER_ID + "=" + circleMemberId;
		return new BillAndDetailDao().getTotalCost(where);
	}



	/**
	 * 通过账单ID得到账单详情<br>
	 * @method getBillViewDetailForBill
	 * @param billId
	 * @return
	 * @throws 
	 * @since v1.0
	 */
	public List<BillViewDetail> getBillViewDetailForBill(long billId){
		List<BillViewDetail> list = new ArrayList<BillViewDetail>();
		String where = BillViewMetaData.BILL_ID + "=" + billId;
		Utils.log(TAG, "where = " + where);
		Cursor cur = new BillAndDetailDao().getAllBillDetail(where , BillViewMetaData.MONEY + " desc");
		if(cur != null && cur.getCount() != 0){
			for(cur.moveToFirst() ; !cur.isAfterLast() ; cur.moveToNext()){
				list.add(new BillViewDetail(cur));
			}
			cur.close();
		}
		return list;
	}



	/**
	 * 通过成员ID得到账单详情<br>
	 * @method getBillViewDetailForMember
	 * @param memberId
	 * @return
	 * @throws 
	 * @since v1.0
	 */
	public List<BillViewDetail> getBillViewDetailForMember(long memberId){
		List<BillViewDetail> list = new ArrayList<BillViewDetail>();
		String where = BillDetailTableMetaData.CIRCLE_MEMBER_ID + "=" + memberId;
		Utils.log(TAG, "where = " + where);
		Cursor cur = new BillAndDetailDao().getAllBillDetail(where , BillTableMetaData.CREATE_AT + " desc");
		if(cur != null && cur.getCount() != 0){
			for(cur.moveToFirst() ; !cur.isAfterLast() ; cur.moveToNext()){
				list.add(new BillViewDetail(cur));
			}
			cur.close();
		}
		return list;
	}

	
	
	public void deleteBillListFromCircle(long billId){
		new BillAndDetailDao().deleteBill(billId);
	}
	
	

}

